package com.bs.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.bs.bean.Item;
import com.bs.service.ItemService;
import com.bs.service.PersonService;

@SuppressWarnings("serial")
@ParentPackage("struts-default")
@Namespace("/item")
public class ItemAction extends ActionBase {
	private Item item;
	private ItemService itemService;
	private PersonService personService;
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public ItemService getItemService() {
		return itemService;
	}
	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}
	
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	@Action("save")
	public void save() throws Exception{
		try {
			this.itemService.save(item);
			response.getWriter().write(this.successInfo("添加项目信息成功"));
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	@Action("delete")
	public void batchDelete() throws Exception{
		try {
			String ids = (String) request.getParameter("ids");
			int rows = this.itemService.batchDelete(ids);
			if(rows>=0){
				response.getWriter().write(this.successInfo("删除项目信息成功"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("findAllForPage")
	public void findAllForPage() throws Exception{
		try {
			int start = Integer.parseInt(request.getParameter("start"));
			int limit = Integer.parseInt(request.getParameter("limit"));
			List<Item> list = this.itemService.findAll(start,limit);
			JSONArray items = JSONArray.fromObject(list);
			int count = this.itemService.totalCount();
			JSONObject obj = new JSONObject();
			obj.put("totalCount", count);
			obj.put("items", items);
			response.getWriter().write(obj.toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("findAllBySeason")
	public void findAllBySeason() throws Exception{
		try {
			String type = request.getParameter("type");
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			List<Item> list = this.itemService.findAll(start, end, type);
			JSONArray items = JSONArray.fromObject(list);
			response.getWriter().write(items.toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("findAll")
	public void findAll() throws Exception{
		try {
			List<Item> list = this.itemService.findAll();
			JSONArray items = JSONArray.fromObject(list);
			response.getWriter().write(items.toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	
	@Action("update")
	public void update()  throws Exception{
		try {
			String type = request.getParameter("type");
			int rows = this.itemService.update(item, type);
			if(rows == 1){
				response.getWriter().write(this.successInfo("更新项目信息成功"));
			}else if(rows == 0){
				response.getWriter().write(this.successInfo("该条项目记录不存在"));
			}
			
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("sumBySeason")
	public void sumBySeason() throws Exception{
		try {
			String type = request.getParameter("type");
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			List<Item> list = this.itemService.sumBySeason(start, end, type);
			JSONArray items = JSONArray.fromObject(list);
			response.getWriter().write(items.toString());
		} catch (IOException e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	@Action("bonusBySeason")
	public void bonusBySeason() throws Exception{
		try {
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			String type = request.getParameter("type");
			List<LazyDynaBean> list = new ArrayList<LazyDynaBean>();
			if(type.equals("busi")){
				list = this.itemService.busiBonusBySeason(start, end);
			}else if(type.equals("support")){
				list = this.itemService.supportBonusBySeason(start, end);
			}else if(type.equals("manager")){
				list = this.itemService.managerBonusBySeason(start, end);
			}else if(type.equals("team")){
				list = this.itemService.teamBonusBySeason(start, end);
			}
			response.getWriter().write(JSONArray.fromObject(list).toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("personBonus")
	public void personBonus() throws Exception{
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		List<LazyDynaBean> list = this.itemService.sumAllPersonBySeason(start, end);
		response.getWriter().write(JSONArray.fromObject(list).toString());
	}
	
	@Action("query")
	public void query() throws IOException{
		List<Item> list = this.itemService.query(item);
		response.getWriter().write(JSONArray.fromObject(list).toString());
 	}
}
